// 日期功能 by  Liu Shuan
Core.lib.date = function() {
	// 格式化日期
	this.formatDate = function(date, style) {
		style = style || "yyyy-MM-ddThh:mm:ss";
		return style.replace(/y+|M+|d+|h+|m+|s+|w+|q+|S/g, function($0) {
			var temp = 0;
			switch ($0.charAt(0)) {
				case "y" :
					return String(date.getFullYear()).substr(4 - $0.length);
				case "w" :
					return "日一二三四五六".charAt(date.getDay());
				case "q" :
					return Math.floor((date.getMonth() + 3) / 3);
				case "S" :
					return date.getMilliseconds();

				case "M" :
					temp = date.getMonth() + 1;
					break;
				case "d" :
					temp = date.getDate();
					break;
				case "h" :
					temp = date.getHours();
					break;
				case "m" :
					temp = date.getMinutes();
					break;
				case "s" :
					temp = date.getSeconds();
					break;
			}

			return (temp < 10 && $0.length == 2) ? "0" + temp : temp;
		});
	}

	var cache1 = {};
	var cache2 = {};

	// 解析出日期
	this.parseDate = function(text, style) {

		style = style || "y-M-d";

		// 匹配的模板 || 兼缓存
		var pattern = cache1[style] || (cache1[style] = RegExp().compile(style.replace(/([Mdyhms])/g, "(\\d+)")));

		// 匹配的顺序 || 兼缓存
		var order = cache2[style]|| (cache2[style] = ("^" + style.replace(/[^Mdyhms]/g, '')).split(''));

		// 匹配的项目 || 不可缓存
		var match = pattern.exec(text);

		// 匹配的结果 （顺序对应项目）
		var result = {}
		for (var i = 1; i < order.length; i++) {
			result[order[i]] = match[i];
		}

		// 按照构造参数进行构造
		var args = "M/d/y h:m:s".replace(/[Mdyhms]/g, function($0) {return result[$0] || 0;});

		return new Date(args);
	}
}

/* 
 * *----------------*
 *  Require
 * *----------------*
 */
$$.require = function(_package){
	var filePath = true;
	
	if(!_package){
		throw "必须输入args对象！";
	}else if(arguments.length > 1){
    	arguments.callee.reduce(arguments);
	}else if(
		(typeof _package == "function")
		||
		(_package.constructor && typeof _package.constructor == "function"  && !_package._tokenString)
		||
		(_package._loaded == true)// _package.prototype.loaded is token to know if loaded
		||
		(_package.prototype && _package.prototype._loaded == true)
	){
    	return _package; // 已经加载了，不重复加载
	
    }
    
    if (typeof _package == 'string'){
		filePath = _package;				// 传入字符串就是资源的路径。
	}else if(_package &&  _package._tokenString){
		filePath = _package._tokenString;	// get token by Static member
	}else if(_package && !_package._tokenString){
		throw '对象上没有 _tokenString 属性！请检查symbol.js文件设置！';
	}

    filePath += ".js";
    arguments.callee.eval_JS_File(filePath);
    
    return filePath; // done.
}
// prototype
$$.require.eval_JS_File = function(file){

}
;(function(){
    function eval_JS_File_MS_ASP_JScript(filePath){
    	
	// for MS ASP Boost file
	    Response.Charset		= "utf-8";
	    Session.CodePage		= 65001;
	    Session.Timeout			= 200;
	    Server.ScriptTimeout	= 100;
    
        if(arguments.length > 1){
            argsList(arguments, arguments.callee);
        }else{
            with (new ActiveXObject("Adodb.Stream")){
                type = 2;
                mode = 3;
                charset = "utf-8";
                open();
                if(typeof($$.console) != 'undefined' && $$.console.log){
                	$$.console.log(filePath);
                	$$.console.log(Server.Mappath(filePath));
                }
                loadFromFile(Server.Mappath(filePath));
                var source = readText();
                eval(source);
                close();
            }
        }
    }
    function eval_JS_File_Browser(src){
        if(arguments.length > 1){
            argsList(arguments, arguments.callee);
        }else{
        	var scriptTag = '<scr' + 'ipt src="{0}"></sc' + 'ript>';
        	
        	if($$.env.isHTA){ // HTA环境下不支持“/”路径，只支持相对路径。
        		scriptTag = '<scr' + 'ipt src="http://localhost/{0}"></sc' + 'ript>';
        	}
            document.write(scriptTag.format(src));
        }
    }
    // @todo
    function eval_JS_File_NodeJS(file){
    }
    
    if(typeof Server != 'undefined'){
	    $$.require.eval_JS_File = eval_JS_File_MS_ASP_JScript;
    }else if(document){
    	$$.require.eval_JS_File = eval_JS_File_Browser;
    }
})();

$$.utils = {
    Template : {
        _tokenString : "/edk/util/template"
    }
    ,JSON : {
    	_tokenString : "/edk/util/json"
    }
};

$$.data = {
	Tree : {
	    _tokenString : "/edk/util/data"
	}
};

$$.Server = {
	IO : {
	    _tokenString : "/edk/server/io"
	}
	,request : {
	    _tokenString : "/edk/server/request"
	}
};

$$.tags = {
    _tokenString : "/deepcms/site/tags"
}

$$.config = {
    cfg : {
        _tokenString : "/deepcms/config/config"
    }
	,webStructure : {
		 _tokenString : "/deepcms/config/structure"
	}
};

$$.mail = {};
$$.mail._tokenString = "/deepcms/mail/email";

$$.upload = {
    _tokenString : "/deepcms/upload/upload"
}   

$$.fs = {
    File : {   
        _tokenString : "/edk/file/fso"
    }   

    ,Folder : {
        _tokenString : "/deepcms/fs/Folder"
    }   
};

$$.page = {
    _tokenString : "/deepcms/site/page"
}

$$.sql = {
	 DAO : {
		_tokenString : "/edk/sql/dao"
	}
	,reader : {
		Reader : {
			_tokenString : "/edk/sql/reader/reader"
		}
		,Pagination : {
			_tokenString : "/edk/sql/reader/page"
		}
	}
	,writer : {
		Writer:{
			_tokenString : "/edk/sql/writer/writer"	
		}
	}
};

$$.data.Entity = {
	_tokenString : "/deepcms/domain/entity"	
}

$$.domain = {
	News : {
		_tokenString : "/deepcms/domain/news/news"	
	}
	,User : {
		_tokenString : "/deepcms/domain/user/user"	
	}
};
$$.siteStructure = {
	_tokenString : "/deepcms/config/structure"	
}


// -----------------------------------
/*
 * 测试期间有用。
 */
$$.ServersideConsole = {
	_tokenString : "/edk/log/console"	
};
$$.require($$.utils.JSON);
$$.require($$.ServersideConsole); // import immeadlity
// -------------------------------

;(function(obj){
	// @todo
	// add a PATH String to every leaf!
})($$);